package com.bw.z.mvp.model;

import com.bw.z.db.UserTable;
import com.bw.z.mvp.HttpCode;
import com.bw.z.mvp.base.model.BaseModel;
import com.bw.z.mvp.contract.InformationContract;
import com.bw.z.mvp.model.entity.BaseEntity;
import com.bw.z.mvp.model.entity.FileEntity;
import com.bw.z.network.Api;
import com.bw.z.network.ChangeFunction;
import com.bw.z.network.HttpFactory;
import com.bw.z.network.HttpType;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class InformationModel extends BaseModel implements InformationContract.informationModel {

    @Inject
    public InformationModel() {
    }

    @Override
    public Observable<BaseEntity> requestFile(Map<String, Object> map) {
        int code = (int) map.get("code");
        map.remove("code");
        if (code == HttpCode.FILECODE){
            List<MultipartBody.Part> list = (List<MultipartBody.Part>) map.get("list");
            map.remove("list");
            Observable<FileEntity> ob = HttpFactory.getInstance().factory(HttpType.UPLOADTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestFile(list);
            return ob.map(new ChangeFunction<FileEntity>());
        }else if (code == HttpCode.UPDATACODE){
            RequestBody body = createBody(map);
            Observable<UserTable> ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestUpdateUser(body);
            return ob.map(new ChangeFunction<UserTable>());
        }
        return null;
    }
}
